﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;

namespace CheemsControl.Controls
{
    public class MessageReminderRadioButton : RadioButton
    {
        static MessageReminderRadioButton()
        {
            // 重写 FontSize 属性的元数据，添加回调方法
            FontSizeProperty.OverrideMetadata(
                typeof(MessageReminderRadioButton),
                new FrameworkPropertyMetadata(OnMessageChanged));
        }


        /// <summary>
        /// 圆角
        /// </summary>
        public CornerRadius CornerRadius
        {
            get { return (CornerRadius)GetValue(CornerRadiusProperty); }
            set { SetValue(CornerRadiusProperty, value); }
        }
        public static readonly DependencyProperty CornerRadiusProperty =
            DependencyProperty.Register("CornerRadius", typeof(CornerRadius), typeof(MessageReminderRadioButton), new PropertyMetadata(new CornerRadius(5)));

        /// <summary>
        /// 是否打开动画
        /// </summary>
        public bool IsAnimationEnabled
        {
            get { return (bool)GetValue(IsAnimationEnabledProperty); }
            set { SetValue(IsAnimationEnabledProperty, value); }
        }
        public static readonly DependencyProperty IsAnimationEnabledProperty =
            DependencyProperty.Register("IsAnimationEnabled", typeof(bool), typeof(MessageReminderRadioButton), new PropertyMetadata(false));

        /// <summary>
        /// 消息文本区域
        /// </summary>
        public object MessageContent
        {
            get { return (object)GetValue(MessageContentProperty); }
            set { SetValue(MessageContentProperty, value); }
        }
        public static readonly DependencyProperty MessageContentProperty =
            DependencyProperty.Register("MessageContent", typeof(object), typeof(MessageReminderRadioButton), new PropertyMetadata(null));

        /// <summary>
        /// 消息文本
        /// </summary>
        public string Message
        {
            get { return (string)GetValue(MessageProperty); }
            set { SetValue(MessageProperty, value); }
        }
        public static readonly DependencyProperty MessageProperty =
            DependencyProperty.Register("Message", typeof(string), typeof(MessageReminderRadioButton), new PropertyMetadata(string.Empty, OnMessageChanged));

        /// <summary>
        /// 消息文本字体颜色
        /// </summary>
        public Brush MessageForeground
        {
            get { return (Brush)GetValue(MessageForegroundProperty); }
            set { SetValue(MessageForegroundProperty, value); }
        }
        public static readonly DependencyProperty MessageForegroundProperty =
            DependencyProperty.Register("MessageForeground", typeof(Brush), typeof(MessageReminderRadioButton), new PropertyMetadata(new SolidColorBrush(Color.FromArgb(0xFF, 0xFF, 0xFF, 0xFF)), OnMessageChanged));


        /// <summary>
        /// 消息文本背景色
        /// </summary>
        public Brush MessageBackground
        {
            get { return (Brush)GetValue(MessageBackgroundProperty); }
            set { SetValue(MessageBackgroundProperty, value); }
        }
        public static readonly DependencyProperty MessageBackgroundProperty =
            DependencyProperty.Register("MessageBackground", typeof(Brush), typeof(MessageReminderRadioButton), new PropertyMetadata(new SolidColorBrush(Color.FromArgb(0xFF, 0xFF, 0xFF, 0xFF)), OnMessageChanged));


        /// <summary>
        /// 消息文本圆角
        /// </summary>
        public CornerRadius MessageCornerRadius
        {
            get { return (CornerRadius)GetValue(MessageCornerRadiusProperty); }
            set { SetValue(MessageCornerRadiusProperty, value); }
        }
        public static readonly DependencyProperty MessageCornerRadiusProperty =
            DependencyProperty.Register("MessageCornerRadius", typeof(CornerRadius), typeof(MessageReminderRadioButton), new PropertyMetadata(new CornerRadius(3), OnMessageChanged));



        /// <summary>
        /// 消息文本改变
        /// </summary>
        /// <param name="d"></param>
        /// <param name="e"></param>
        /// <exception cref="NotImplementedException"></exception>
        private static void OnMessageChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            MessageReminderRadioButton thisControl = d as MessageReminderRadioButton;
            if (string.IsNullOrEmpty(thisControl.Message))
            {
                thisControl.MessageContent = null;
                return;
            }
            else
            {
                Border border = new Border()
                {
                    Background = thisControl.MessageBackground,
                    CornerRadius = thisControl.MessageCornerRadius,
                    MinWidth = thisControl.FontSize * 0.7,
                    MinHeight = thisControl.FontSize * 0.7,
                    VerticalAlignment = VerticalAlignment.Center,
                };
                TextBlock textBlock = new TextBlock()
                {
                    Text = thisControl.Message,
                    FontSize = thisControl.FontSize * 0.5,
                    Foreground = thisControl.MessageForeground,
                    VerticalAlignment = VerticalAlignment.Center,
                    HorizontalAlignment = HorizontalAlignment.Center,
                    Margin = new Thickness(thisControl.FontSize * 0.1),
                };
                border.Child = textBlock;

                thisControl.MessageContent = border;
            }

        }
    }
}
